﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Models.Sellers;
using S4Store.Service;
using S4Store.Domain;


namespace S4Store.Controllers
{
    public class SparePartController : ReportBaseController
    {
        public SparePartService SparePartService { get; set; }

        public ActionResult Index()
        {
            QueryModel query = new QueryModel
            {
                Page = 1,
                PageSize = 20
            };
            return Query(query);
        }

        public ActionResult AddOrUpdate(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(SparePartService);
                var querysource = SparePartService.GetQuery();
                querysource = querysource.Where(x => x.StoreId == Store.Id);
                var queryresult = querysource.Where(o => o.Month.Year == y && o.Month.Month == m).FirstOrDefault();
                if (queryresult == null)
                {
                    queryresult = new Domain.AfterSaleKpi.SparePartsMonthInput();
                }
                queryresult.Month = new DateTime(y, m, 1);
                return View(queryresult);
            }
        }

        public string MonthTd(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(SparePartService);
                var querysource = SparePartService.GetQuery();
                querysource = querysource.Where(x => x.StoreId == Store.Id);
                var queryresult = querysource.Where(o => o.Month.Year == y && o.Month.Month == m).FirstOrDefault();
                if (queryresult == null)
                {
                    return "style='background:" + S4Store.Models.Chart.Color.Red + ";padding:10px;'";
                }
                else if (queryresult.IsConfirmed)
                {
                    return "style='background:" + S4Store.Models.Chart.Color.Green + ";padding:10px'";
                }
                else
                {
                    return "style='background:" + S4Store.Models.Chart.Color.Yellow + ";padding:10px;'";
                }

            }
        }

        [HttpPost]
        public ActionResult Query(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject(SparePartService);

               var querysource = SparePartService.GetQuery();
               int storeid = Store.Id;
               querysource = querysource.Where(x => x.StoreId == storeid);
               IList<Domain.AfterSaleKpi.SparePartsMonthInput> inputs = new List<Domain.AfterSaleKpi.SparePartsMonthInput>();
               for (int i = 0; i < 72; i++)
               {
                    DateTime month = DateTime.Now.AddMonths(-i);
                    Domain.AfterSaleKpi.SparePartsMonthInput tempSpmi = querysource.Where(o => o.Month.Year == month.Year && o.Month.Month == month.Month).FirstOrDefault();
                    if (tempSpmi == null)
                    {
                        tempSpmi = new Domain.AfterSaleKpi.SparePartsMonthInput();
                        tempSpmi.Month = month;
                        inputs.Add(tempSpmi);
                    }
                    else
                    {
                        inputs.Add(tempSpmi);
                    }
               }

               return View(inputs);
            }
        }

        public JsonResult SaveAddOrUpdateSparePartMonthInput(S4Store.Domain.AfterSaleKpi.SparePartsMonthInput config, int y, int m, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(SparePartService);
                DateTime now = new DateTime(y, m, 1);
                config.IsConfirmed = isconfirm;
                config.Month = now;
                config.StoreId = Store.Id;
                SparePartService.SubmitSparePartsMonthConfig(y, m, Store.Id, config);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }
    }
}
